package Demo7;
class Money implements Cloneable{
    public double money=12.25;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
class Student implements Cloneable{
    public String name;
    public Money m=new Money();

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                '}';
    }

    @Override
    //调用改方法可能会出现该异常（编译时的异常）
    protected Object clone() throws CloneNotSupportedException {

        Student student =(Student) super.clone();//这行代码只是克隆了一个Student类的对象，用student接收
        student.m=(Money) this.m.clone();//将这个studet的m的对象也克隆一份，并强制转换成Money类型
        return student;//发生向上转型
    }
}
public class Test {
    public static void main1(String[] args) throws CloneNotSupportedException{
        Student student1=new Student();
        student1.name="小猫";
        Student student2=(Student) student1.clone();//发生向下整型
        System.out.println(student1.name);
        System.out.println(student2.name);
    }

    public static void main(String[] args) throws CloneNotSupportedException{
        Student student1=new Student();
        Student student2=(Student) student1.clone();
        System.out.println(student1.m.money);
        System.out.println(student2.m.money);
        System.out.println("_______________");
        student2.m.money=89;
        System.out.println(student1.m.money);
        System.out.println(student2.m.money);
    }
}
